package com.hmall.api.config;

import com.hmall.api.clients.fallback.ItemClientFallbackFactory;
import com.hmall.api.clients.fallback.PayClientFallback;
import com.hmall.common.utils.UserContext;
import feign.Logger;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;

/**
 * openFeign配置
 */
public class DefaultFeignConfig {
    @Bean
    public Logger.Level feignLoggerLevel(){ //配置日志
        return Logger.Level.FULL;
    }
    @Bean
    public RequestInterceptor userInfoRequestInterceptor(){//微服务之间的拦截器，传递用户id
        return new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate requestTemplate) {
                Long userId = UserContext.getUser();//引入common模块，从UserContext获取用户id
                if (userId!=null){
                    requestTemplate.header("user-info", userId.toString());
                }
            }
        };
    }
    @Bean
    public ItemClientFallbackFactory itemClientFallbackFactory(){ //fallback处理逻辑
        return new ItemClientFallbackFactory();
    }
    @Bean
    public PayClientFallback payClientFallback(){
        return new PayClientFallback();
    }
}
